package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zb.entity.ZjCode;
import com.zb.entity.ZjCode03;
import com.zb.entity.ZjCodeInfo03;
import com.zb.entity.ZjCodeLog03;
import com.zb.mapper.ZjCode03Mapper;
import com.zb.service.ZjCode03Service;
import com.zb.service.ZjCodeInfo03Service;
import com.zb.service.ZjCodeLog03Service;
import com.zb.utils.TransactionUtil;
import com.zb.vo.CodeRelateoinInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Author WEN
 * @Date 2023/8/29 0029
 **/
@Service
public class ZjCode03ServiceImpl extends ServiceImpl<ZjCode03Mapper, ZjCode03> implements ZjCode03Service {
    @Resource
    private ZjCode03Mapper code03Mapper;
    @Resource
    private PlatformTransactionManager transactionManager;
    @Resource
    private ZjCodeInfo03Service codeInfo03Service;
    @Resource
    private ZjCodeLog03Service codeLog03Service;

    @Override
    public void save(CountDownLatch countDownLatch, List<ZjCode03> codeList, List<ZjCodeInfo03> codeInfos, List<ZjCodeLog03> codeLogs) {
        if (null == codeList || codeList.isEmpty()) {
            countDownLatch.countDown();
            return;
        }
        TransactionStatus status = transactionManager.getTransaction(TransactionUtil.transactionDefinition());
        try {
            this.saveBatch(codeList);
            codeInfo03Service.saveBatch(codeInfos);
            codeLog03Service.saveBatch(codeLogs);
            transactionManager.commit(status);
        }catch (Exception e) {
            transactionManager.rollback(status);
        }finally {
            countDownLatch.countDown();
        }
    }

    public void save(List<ZjCode03> codeList, List<ZjCodeInfo03> codeInfos, List<ZjCodeLog03> codeLogs) {
        if (null == codeList || codeList.isEmpty()) {
            return;
        }
        this.saveBatch(codeList);
        codeInfo03Service.saveBatch(codeInfos);
        codeLog03Service.saveBatch(codeLogs);
    }

    // 根据编码ID激活编码
    @Override
    public void updateBatchActivCodeByIds(List<ZjCode03> code03s, List<ZjCodeLog03> codeLog03s) {
        if (null == code03s || code03s.isEmpty()) {
            return;
        }
        this.updateBatchById(code03s);
        codeLog03Service.saveBatch(codeLog03s);
    }

    // 根据编码ID删除编码
    @Override
    public void deleteBatchByIds(List<Long> codeIds) {
        if (null == codeIds || codeIds.isEmpty()) {
            return;
        }
        this.removeByIds(codeIds);
        codeInfo03Service.removeByIds(codeIds);
        codeLog03Service.deleteBatchByCodeIds(codeIds);
    }

    // 根据编码批次ID查询检查是否编码有关联信息
    @Override
    public List<CodeRelateoinInfoVo> checkCodeRelationInfoForDeleteByBatchIds(List<Long> codeBatchIds) {
        return code03Mapper.checkCodeRelationInfoForDeleteByBatchIds(codeBatchIds);
    }

    // 根据批次ID获取编码ID
    @Override
    public List<Long> getIdByBatchId(Long batchId) {
        QueryWrapper<ZjCode03> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("batch_id", batchId);
        return this.list(wrapper).stream().map(ZjCode03::getId).toList();
    }

    // 根据编码ID查询检查是否编码有关联信息
    @Override
    public List<CodeRelateoinInfoVo> checkCodeRelationInfoByIds(List<Long> ids) {
        return code03Mapper.checkCodeRelationInfoByIds(ids);
    }
}
